home *** CD-ROM | disk | FTP | other *** search
- Path: newshost.lanl.gov!tanmoy
- From: tanmoy@qcd.lanl.gov (Tanmoy Bhattacharya)
- Newsgroups: comp.lang.c
- Subject: Re: Pre-processing: Can this be done?
- Date: 08 Feb 1996 20:16:52 GMT
- Organization: Los Alamos National Laboratory
- Message-ID: <TANMOY.96Feb8131652@qcd.lanl.gov>
- References: <DMGo4t.7r7@gti-ia.nl>
- NNTP-Posting-Host: qcd.lanl.gov
- Mime-Version: 1.0
- Content-Type: text
- In-reply-to: paulw@gti-ia.nl's message of Thu, 8 Feb 1996 14:26:52 GMT
-
- In article <DMGo4t.7r7@gti-ia.nl> paulw@gti-ia.nl (Paul Wallis)
- writes:
- <snip>
- I'm trying to write a function which will print trace statements.
- I want the file and line to be added to the argument list
- automatically. I am, however, having some problems working
- my way around the pre-processor. I would like to be able to enter
- the following line:
-
- trace("%d, %f", an_int, a_float);
-
- and have the arguments passed as so:
-
- _trace(__FILE__, __LINE__, "%d, %f", an_int, a_float);
-
- There are no varargs macros in portable C.
-
- at the x. Apparently not liking multiple arguments. Is there
- any way to get it to accept this, without having to do this,
- which is very ugly:
-
- trace(("%d, %f", an_int, a_float));
-
- This is usually the preferred solution.
-
- I have also tried redefining a macro as such:
-
- #define trace( _trace(__FILE__, __LINE__
-
- but to no avail. Is there an answer, or am I stuck with the
- ugly?
-
- This, as you discovered, does not work. You either have object like
- macros, or function like macros: no comlicated constructs.
-
- Thanks in advance for any help,
-
- If you want to trade one ugliness for another, you may use the
- following:
-
- extern void record(const char*,int);
- #define trace record(__FILE__,__LINE__); trace
-
-
- and in another file
-
- static const char *file;
- static int line;
-
- void record(const char *lastfile, int lastline) {
- file=lastfile; /* We are assuming we do not need to copy string */
- line=lastline;
- }
-
- void trace(const char *format, ...) {
- /* uses the file scope variables file and line */
- file=NULL; line=0; /* to catch cases where trace directly called
- instead of through macro */
- }
-
- Cheers
- Tanmoy
- --
- tanmoy@qcd.lanl.gov(128.165.23.46) DECNET: BETA::"tanmoy@lanl.gov"(1.218=1242)
- Tanmoy Bhattacharya O:T-8(MS B285)LANL,NM87545 H:#9,3000,Trinity Drive,NM87544
- Others see <gopher://yaleinfo.yale.edu:7700/00/Internet-People/internet-mail>,
- <http://alpha.acast.nova.edu/cgi-bin/inmgq.pl>or<ftp://csd4.csd.uwm.edu/pub/
- internetwork-mail-guide>. -- <http://nqcd.lanl.gov/people/tanmoy/tanmoy.html>
- fax: 1 (505) 665 3003 voice: 1 (505) 665 4733 [ Home: 1 (505) 662 5596 ]
-